#include "MEFXMLTree.h"
#include "MEFXMLParser.h"
#include "MEFXMLTag.h"

CMEFXMLTree::CMEFXMLTree() : iParser(0), /*iRoot(0), iCurrent(0),*/ iTree(0)
{
	iTree = new CMEFTree<CMEFXMLTag*>();
	iTree->value = 0;
}

CMEFXMLTree::~CMEFXMLTree()
{
	delete iParser;

	iIt = iTree->begin();
	while (iIt != iTree->end())
	{
		delete iIt->value; 
		iIt++;
	}
	delete iTree;

	//for(unsigned int i = 0; i < iNodes.Size(); i++)
	//{
	//	delete iNodes.At(i);
	//}
	//for(unsigned int i = 0; i < iTags.Size(); i++)
	//{
	//	delete iTags.At(i);
	//}
	//TODO:
	//delete all nodes
}


void CMEFXMLTree::BuildTree(const CMEFString& aFileName)
{
	iParser = new CMEFXMLParser();
	iParser->ParseFile(aFileName, *this);
}

CMEFTree<CMEFXMLTag*>& CMEFXMLTree::RootTree()
{
	assert(iTree);
	if(iTree)
		return *iTree;
}


void CMEFXMLTree::OnStartElement(const CMEFString& aType,  const CMEFStringHash& aAttributes)
{
	//Create new nodes with content
	CMEFXMLTag* tag = new CMEFXMLTag(aType, aAttributes);
	
	if(iTree->value == 0)
	{
		iTree->value = tag;
		iIt = iTree->begin();
	}
	else
	{
		iIt->push_back(tag);
		iIt = iIt->back().begin();
	}

	//iTags.PushBack(tag);
	//CMEFNode<CMEFXMLTag*>* node = new CMEFNode<CMEFXMLTag*>(tag);
	//iNodes.PushBack(node);

	//if (iCurrent != 0)
	//{
	//	iCurrent->AddChild(node);
	//}
	//else
	//{
	//	iRoot = node;
	//	iCurrent = node;
	//}

	//iOpenTagStack.Push(iCurrent);
}

void CMEFXMLTree::OnEndElement(const CMEFString& aType)
{
	if(iIt->parent())
		iIt = iIt->parent()->begin();
	//iOpenTagStack.Pop();
}

void CMEFXMLTree::OnText(const CMEFString& aText)
{
}



